package org.argus.management.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.argus.common.core.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.argus.common.core.enums.StatusEnum;
import org.argus.common.web.core.snowflake.SnowflakeIdGenerator;
import org.argus.management.domain.Role;
import org.argus.management.system.mapper.RoleMapper;
import org.argus.management.system.model.RoleMenuBindReq;
import org.argus.management.system.model.RoleSearchReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 刘文/Cliff Liu
 * @Date 2024/6/3
 */
@Service
public class RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    public Page<Role> search(RoleSearchReq req, Page page) {
        LambdaQueryWrapper<Role> queryWrapper = Wrappers.lambdaQuery(Role.class);
        if (StringUtils.isNotBlank(req.getName())) {
            queryWrapper.like(Role::getName, req.getName());
        }
        if (StringUtils.isNotBlank(req.getCode())) {
            queryWrapper.like(Role::getCode, req.getCode());
        }
        if (StringUtils.isNotBlank(req.getStatus())) {
            queryWrapper.eq(Role::getStatus, req.getStatus());
        }
        return roleMapper.selectPage(page, queryWrapper);
    }

    public void updateStatus(Role role) {
        LambdaUpdateWrapper<Role> updateWrapper = Wrappers.lambdaUpdate(Role.class).set(Role::getStatus, role.getStatus()).eq(Role::getId, role.getId());
        roleMapper.update(null, updateWrapper);
    }

    public void save(Role role) {
        int duplicateCount = roleMapper.checkNameCodeDuplicateCount(role);
        if (duplicateCount > 0) {
            throw new ServiceException("角色名称或编码重复");
        }
        if (role.getId() == null) {
            role.setId(snowflakeIdGenerator.nextId());
            roleMapper.insert(role);
        }else{
            roleMapper.updateById(role);
        }
    }

    public void delete(Long id) {
        int userCount = roleMapper.getRoleUserCount(id);
        if (userCount > 1) {
            throw new ServiceException("该角色正在被使用, 无法删除");
        }
        roleMapper.deleteById(id);
    }

    public List<Long> roleMenuIds(Long id) {
        return roleMapper.roleMenuIds(id);
    }

    public void roleMenuBind(RoleMenuBindReq req) {
        Role role = roleMapper.selectById(req.getRoleId());
        if (role == null) {
            throw new ServiceException("角色不存在");
        }

        roleMapper.roleMenuBind(req);
    }

    public List<Role> listAllRole() {
        return roleMapper.selectList(Wrappers.lambdaQuery(Role.class).eq(Role::getStatus, StatusEnum.NORMAL));
    }
}
